* ==============================================================================
* Data: experimental data 
* Project: Multiple Switching and Data Quality in the Multiple Price List
*    
*
* Last updated: September 2019
* ==============================================================================

********************************************************************************
*** Creates bootstrap samples and statistics for Tables 4, 5, 6, A.6, Figure A.1
*** and permutation test sample for Figure 2
********************************************************************************

clear all
set more off
cap log close

global path "//your/path/"
cd "$path"
use "data.dta"
do "data_cleaning_and_recoding.do"

* Create boostrap sample for Table 4, Table 5 and Figure A.1
********************************************************************************
sort nudge id
putmata ls = ls, replace

set seed 793986710 // generated by random.org
tempname N_0 N_1 bs_results

count if nudge==0
scalar `N_0' = r(N)
count if nudge==1
scalar `N_1' = r(N)

postfile `bs_results' id N_0 N_1 S_0 S_1 CorrS_0 CorrS_1 SP P_CORR using "test1_bs_results.dta", replace

display "running bootstrap samples "
forvalues i = 1/10000 {
	if mod(`i',100)==0 {
		display _continue "`i'... "
	}
	quietly{
		tempname S_0 S_1 CorrS_0 CorrS_1 SP P_CORR

		preserve
		generate nsample = cond(nudge,`N_1',`N_0')
		bsample nsample, strata(nudge)
		drop ls
		getmata ls
		covtest mpl_fs ls, by(nudge)
		foreach var in S_0 S_1 CorrS_0 CorrS_1 SP P_CORR {
			scalar ``var'' = r(`var')
		}
		post `bs_results' (`i') (`N_0') (`N_1') (`S_0') (`S_1') (`CorrS_0') (`CorrS_1') (`SP') (`P_CORR')
		
		restore
	}
}
postclose `bs_results'	

preserve
use "test1_bs_results.dta", clear
summarize
restore

* Boostrap sample for Table 6
********************************************************************************
sort nudge id
putmata ls = ls if ls_first==1, replace

set seed 130686426 // generated by random.org
tempname N_0 N_1 bs_results

count if nudge==0 & ls_first==1
scalar `N_0' = r(N)
count if nudge==1 & ls_first==1
scalar `N_1' = r(N)

postfile `bs_results' id N_0 N_1 S_0 S_1 CorrS_0 CorrS_1 SP P_CORR using "test_lsfirst_bs_results.dta", replace

display "running bootstrap samples "
forvalues i = 1/10000 {
	if mod(`i',100)==0 {
		display _continue "`i'... "
	}
	quietly{
		tempname S_0 S_1 CorrS_0 CorrS_1 SP P_CORR
		tempfile bs_sample

		preserve
		keep if ls_first==1
		generate nsample = cond(nudge,`N_1',`N_0')
		bsample nsample, strata(nudge)
		drop ls
		getmata ls
		covtest mpl_fs ls, by(nudge)
		foreach var in S_0 S_1 CorrS_0 CorrS_1 SP P_CORR {
			scalar ``var'' = r(`var')
		}
		post `bs_results' (`i') (`N_0') (`N_1') (`S_0') (`S_1') (`CorrS_0') (`CorrS_1') (`SP') (`P_CORR')
		
		restore
	}
}
postclose `bs_results'	

* Permutation sample for Figure 2
********************************************************************************
sort nudge id
set seed 1485369220 // generated by random.org

tempname N_0_ls_first N_1_ls_first pm_results

count if nudge==0 & ls_first==1
scalar `N_0_ls_first' = r(N)
count if nudge==1 & ls_first==1
scalar `N_1_ls_first' = r(N)

postfile `pm_results' id n_0_ls_first n_1_ls_first diff using "pm_order_results.dta", replace

display "running permutation test samples"
forvalues i = 1/10000 {
	if mod(`i',100)==0 {
		display _continue "`i'... "
	}
	quietly{
		tempname diff

		preserve

		capture drop ls_first
		generate random_order = runiform()
		sort nudge random_order
		generate N_ls_first=cond(nudge,`N_1_ls_first',`N_0_ls_first')
		by nudge: generate ls_first= N_ls_first >= _n

		covtest mpl_fs ls if ls_first==0, by(nudge)
		local diff_mpl_first = r(S_1) - r(S_0)
		covtest mpl_fs ls if ls_first==1, by(nudge)
		local diff_ls_first = r(S_1) - r(S_0)
		local diff = `diff_ls_first' - `diff_mpl_first'

		post `pm_results' (`i') (`N_0_ls_first') (`N_1_ls_first') (`diff')
		
		restore
	}
}
postclose `pm_results'	

* Boostrap sample for Table A.6
********************************************************************************
sort nudge id
putmata ls = ls if above_med==1, replace

set seed 1784122937 // generated by random.org

tempname N_0 N_1 bs_results

count if nudge==0 & above_med==1
scalar `N_0' = r(N)
count if nudge==1 & above_med==1
scalar `N_1' = r(N)

postfile `bs_results' id N_0 N_1 S_0 S_1 CorrS_0 CorrS_1 SP P_CORR using "abovemedian_bs_results.dta", replace

display "running bootstrap samples "
forvalues i = 1/10000 {
	if mod(`i',100)==0 {
		display _continue "`i'... "
	}
	quietly{
		tempname S_0 S_1 CorrS_0 CorrS_1 SP P_CORR
		tempfile bs_sample

		preserve
		keep if above_med==1
		generate nsample = cond(nudge,`N_1',`N_0')
		bsample nsample, strata(nudge)
		drop ls
		getmata ls

		covtest mpl_fs ls, by(nudge)
		foreach var in S_0 S_1 CorrS_0 CorrS_1 SP P_CORR {
			scalar ``var'' = r(`var')
		}
		post `bs_results' (`N_0') (`N_1') (`i') (`S_0') (`S_1') (`CorrS_0') (`CorrS_1') (`SP') (`P_CORR')
		
		restore
	}
}
postclose `bs_results'	